package com.shujia;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisCon {


    public Jedis getRedis(){
        Jedis jedis = new Jedis("192.168.20.90", 6379);
        System.out.println("redis 已连接...");
        return jedis;
    }

    @Test
    public void getAllKeys(){
        Jedis redis = getRedis();
        Set<String> keys = redis.keys("*");
        for (String key : keys) {
            System.out.println("key..."+key);
        }
    }


    @Test
    public void StringOpera(){
        Jedis redis = getRedis();
        redis.mset("strk1","strv1","Strk1","Strv1");
        List<String> mget = redis.mget("strk1", "Strk1");
        for (String s : mget) {
            System.out.println(s);
        }
    }

    @Test
    public void SetOpera(){
        Jedis jedis = getRedis();
        jedis.sadd("orders", "order01","order04","order02","order03");
        Set<String> smembers = jedis.smembers("orders");
        for (String order : smembers) {
            System.out.println(order);
        }
        jedis.srem("orders", "order02");
    }

    @Test
    public void ListOpera(){
        Jedis jedis = getRedis();
        jedis.lpush("mylist","listV1","listV2","listV3","listV4");
        List<String> list = jedis.lrange("mylist",0,-1);
        for (String element : list) {
            System.out.println(element);
        }
    }

    @Test
    public void HashOpera(){
        Jedis jedis = getRedis();
        jedis.hset("hash1","userName","lisi");
        System.out.println(jedis.hget("hash1","userName"));
        Map<String,String> map = new HashMap<String,String>();
        map.put("clazz","bigdata19");
        map.put("name","huazi");
        map.put("age","23");
        jedis.hmset("hash2",map);
        List<String> result = jedis.hmget("hash2", "clazz","name");
        for (String element : result) {
            System.out.println(element);
        }
    }

    @Test
    public void ZSetOpera(){
        Jedis jedis = getRedis();

        jedis.zadd("zset01", 100, "z3");
        jedis.zadd("zset01", 90, "l4");
        jedis.zadd("zset01", 80, "w5");
        jedis.zadd("zset01", 70, "z6");

        Set<String> zrange = jedis.zrange("zset01", 0, -1);
        for (String e : zrange) {
            System.out.println(e);
        }
    }

    


}
